﻿using System;
using System.IO;

namespace EditoneLib.Streams
{
    public class ZeroStream : ReadonlyStream
    {
        private readonly long _length;

        public ZeroStream(long length)
        {
            _length = length;
        }

        public override long Length
        {
            get { return _length; }
        }

        public override long Position { get; set; }

        public override int Read(byte[] buffer, int offset, int count)
        {
            Array.Clear(buffer, offset, count);

            long bytesRead = Math.Min(count, _length - Position);
            Position += bytesRead;

            return (int) bytesRead;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            switch (origin)
            {
                case SeekOrigin.Begin:
                    Position = offset;
                    break;

                case SeekOrigin.Current:
                    Position += offset;
                    break;

                case SeekOrigin.End:
                    Position = Length + offset;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("origin");
            }
            return Position;
        }
    }
}